package com.jstyle.jclifexd.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.haibin.calendarview.ExerciseRecodeData;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.activity.HealthHistoryActivity;
import com.jstyle.jclifexd.activity.HeartHistoryActivity;
import com.jstyle.jclifexd.activity.HeartTipsActivity;
import com.jstyle.jclifexd.activity.HistoryActivity;
import com.jstyle.jclifexd.adapter.EcgShowAdapter;
import com.jstyle.jclifexd.adapter.MyViewPagerAdapter;
import com.jstyle.jclifexd.daoManager.HealthDataDaoManager;
import com.jstyle.jclifexd.daoManager.HeartDataDaoManager;
import com.jstyle.jclifexd.model.HealthData;
import com.jstyle.jclifexd.model.HeartData;
import com.jstyle.jclifexd.utils.BloodMaxComparator;
import com.jstyle.jclifexd.utils.BloodMinComparator;
import com.jstyle.jclifexd.utils.ChartDataUtil;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.HealthDataUtils;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.utils.Utils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link EcgHistoryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class EcgHistoryFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    @BindView(R.id.CalendarLayout)
    com.haibin.calendarview.CalendarLayout CalendarLayout;
    Unbinder unbinder;
    @BindView(R.id.CalendarView_sleep_Month)
    CalendarView CalendarViewSleepMonth;
    @BindView(R.id.tv_health_heart)
    TextView tvHealthHeart;
    @BindView(R.id.tv_health_hr)
    TextView tvHealthHr;
    @BindView(R.id.LineChartView_health_heart)
    LineChartView LineChartViewHealthHeart;
    @BindView(R.id.tv_health_blood)
    TextView tvHealthBlood;
    @BindView(R.id.tv_blood_pressure)
    TextView tvBloodPressure;
    @BindView(R.id.tv_blood_data)
    TextView tvBloodData;
    @BindView(R.id.tv_last_blood)
    TextView tvLastBlood;
    @BindView(R.id.tv_blood_avg)
    TextView tvBloodAvg;
    @BindView(R.id.tv_blood_max)
    TextView tvBloodMax;
    @BindView(R.id.tv_blood_avgValue)
    TextView tvBloodAvgValue;
    @BindView(R.id.tv_blood_maxValue)
    TextView tvBloodMaxValue;
    @BindView(R.id.tv_hrv_value)
    TextView tvHrvValue;
    @BindView(R.id.tv_hrv)
    TextView tvHrv;
    @BindView(R.id.tv_stress_value)
    TextView tvStressValue;
    @BindView(R.id.tv_body_pressure)
    TextView tvBodyPressure;
    @BindView(R.id.RadioButton_stress1)
    RadioButton RadioButtonStress1;
    @BindView(R.id.RadioButton_stress2)
    RadioButton RadioButtonStress2;
    @BindView(R.id.RadioButton_stress3)
    RadioButton RadioButtonStress3;
    @BindView(R.id.RadioButton_stress4)
    RadioButton RadioButtonStress4;
    @BindView(R.id.tv_avblock_value)
    TextView tvAvblockValue;
    @BindView(R.id.tv_AV_BLOCK)
    TextView tvAVBLOCK;
    @BindView(R.id.iv_av_block)
    ImageView ivAvBlock;
    @BindView(R.id.tv_af)
    TextView tvAf;
    @BindArray(R.array.avblock_level)
    String[] blockLevels;
    //    @BindView(R.id.space)
//    LinearLayout space;

    //    @BindView(R.id.recyclerView_health_heart)
//    RecyclerView recyclerViewHealthHeart;
    @BindView(R.id.bt_ecghistory_date)
    Button btEcghistoryDate;
    @BindView(R.id.viewpager_ecg)
    ViewPager viewpagerEcg;
    @BindView(R.id.ScrollView)
    NestedScrollView ScrollView;
    @BindView(R.id.rl)
    RelativeLayout rl;
    @BindView(R.id.RecyclerView_ecgDatas)
    RecyclerView RecyclerViewEcgDatas;
    @BindView(R.id.viewpager_rl)
    RelativeLayout viewpagerRl;
    @BindView(R.id.RadioGroup_stress)
    RadioGroup RadioGroupStress;
    @BindView(R.id.iv_date)
    ImageView ivDate;
    @BindView(R.id.iv_hrv_level0)
    ImageView ivHrvLevel0;
    @BindView(R.id.iv_hrv_level1)
    ImageView ivHrvLevel1;
    @BindView(R.id.iv_hrv_level2)
    ImageView ivHrvLevel2;
    @BindView(R.id.iv_hrv_level3)
    ImageView ivHrvLevel3;
    @BindView(R.id.tv_hrv_advice)
    TextView tvHrvAdvice;
    @BindView(R.id.tv_stress_advice)
    TextView tvStressAdvice;
    @BindView(R.id.tv_avblockTips)
    TextView tvAvblockTips;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    private Typeface mFace;
    @BindArray(R.array.string_hrv_array)
    String[] hrvContent;
    @BindArray(R.array.string_pressure_array)
    String[] stressContent;
    @BindArray(R.array.string_bp_array)
    String[] bpContent;
    @BindArray(R.array.color_hrv_array)
    int[] hrvColor;
    @BindArray(R.array.hrv_advice)
    String[] hrvAdvice;
    @BindArray(R.array.stress_advice)
    String[] stressAdvice;
    private MyViewPagerAdapter myViewPagerAdapter;
    private EcgShowAdapter ecgShowAdapter;
    private List<HealthData> todayHealths;

    public EcgHistoryFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment EcgHistoryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static EcgHistoryFragment newInstance(String param1, String param2) {
        EcgHistoryFragment fragment = new EcgHistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ecg_history, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private static final String TAG = "EcgHistoryFragment";

    private void initView() {
        Typeface mFaceb ;
        if(!Utils.IsRussian(getActivity())) {
            mFaceb = Typeface.createFromAsset(getActivity().getAssets(), "ROBOTO-LIGHT_1.TTF");
        }else{
            mFaceb= Typeface.createFromAsset(getActivity().getAssets(), "SourceHanSansCN-Regular.ttf");
        }
        tvHrvAdvice.setTypeface(mFaceb);
        tvStressAdvice.setTypeface(mFaceb);
        tvAvblockTips.setTypeface(mFaceb);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        RecyclerViewEcgDatas.setLayoutManager(linearLayoutManager);
        viewpagerEcg.setPageMargin(ScreenUtils.dip2px(getActivity(), 8));
        viewpagerEcg.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ecgShowAdapter.setCheckPosition(position);
                showHealthData(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        if(!Utils.IsRussian(getActivity())) {
            mFace = Typeface.createFromAsset(getActivity().getAssets(), "Brandon_reg.otf");
        }else{
            mFace= Typeface.createFromAsset(getActivity().getAssets(), "SourceHanSansCN-Regular.ttf");
        }
//
//        findData(nowDate);
        CalendarViewSleepMonth.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Calendar calendar, boolean isClick) {
                Log.i(TAG, "onDateSelected: " + isClick);
                String date = calendar.getYear() + "." + String.format("%02d", calendar.getMonth()) + "." + String.format("%02d", calendar.getDay());
                btEcghistoryDate.setText(date);
                findData(date);
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            CalendarViewSleepMonth.scrollToCurrent();
        }
    }

    private void getEcgData(String date) {
        viewpagerEcg.removeAllViews();
        // List<HealthData> healthDataList = HealthDataDaoManager.queryDayData(date.substring(2));
        int size = todayHealths.size();
        ecgShowAdapter = new EcgShowAdapter(size);
        RecyclerViewEcgDatas.setAdapter(ecgShowAdapter);
        if (size == 0) {
            viewpagerRl.setVisibility(View.GONE);
            rl.setVisibility(View.VISIBLE);
        } else {
            myViewPagerAdapter = new MyViewPagerAdapter(getChildFragmentManager(), size, todayHealths);
            viewpagerEcg.setAdapter(myViewPagerAdapter);
            rl.setVisibility(View.GONE);
            viewpagerRl.setVisibility(View.VISIBLE);
        }


    }

    private void findData(String date) {

        initHeartView(date);
        initHealthData(date);
        getEcgData(date);
    }

    private Calendar getSchemeCalendar(int year, int month, int day) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(Color.RED);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme("");
        return calendar;
    }

    String findDate;

    private void initHealthData(String date) {
        findDate = date;
        String findDate = date.substring(2);
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTimeInMillis(DateUtil.getDefaultLongMi(findDate + " 00:00:00"));
        int maxDay = calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
        int minDay = calendar.getActualMinimum(java.util.Calendar.DAY_OF_MONTH);
        calendar.set(java.util.Calendar.DAY_OF_MONTH, maxDay);
        String maxDaySting = DateUtil.getDefaultFormatTime(calendar.getTime());
        calendar.set(java.util.Calendar.DAY_OF_MONTH, minDay);
        String minDaySting = DateUtil.getDefaultFormatTime(calendar.getTime());
        Map<String, Calendar> map = new HashMap<>();
        String[] todayArray = DateUtil.getEcgFormatTodayString(System.currentTimeMillis()).split("\\.");
        Calendar calendarSchemeToday = getSchemeCalendar(Integer.valueOf(todayArray[0]), Integer.valueOf(todayArray[1]), Integer.valueOf(todayArray[2]));
        map.put(calendarSchemeToday.toString(), calendarSchemeToday);//今天默认灰色
        todayHealths = HealthDataDaoManager.queryDayData(findDate + " 00:00:00", findDate + " 23:59:59");
        List<HealthData> healthDataList = HealthDataDaoManager.queryDayData(minDaySting + " 00:00:00", maxDaySting + " 23:59:59");

        for (HealthData healthData : healthDataList) {
            String time = "20" + healthData.getTime().split(" ")[0];
            String[] dateArray = time.split("\\.");
            ExerciseRecodeData exerciseRecodeData = new ExerciseRecodeData();
            exerciseRecodeData.setEcg(ExerciseRecodeData.HasData);
            Calendar calendarScheme = getSchemeCalendar(Integer.valueOf(dateArray[0]), Integer.valueOf(dateArray[1]), Integer.valueOf(dateArray[2]));
            map.put(calendarScheme.toString(), calendarScheme);
        }
        CalendarViewSleepMonth.setSchemeDate(map);
        if (todayHealths == null || todayHealths.size() == 0) {
            clearData();
            return;
        }

        showHealthData(0);
        int maxHrv = 0;
        int maxPressure = 0;
        int minHrv = 300;
        int minPressure = 300;
        int maxBloodHigh = 0;
        int maxBloodLow = 0;


        for (HealthData healthData1 : healthDataList) {
            int hrv = healthData1.getHrv();
            int pressure = healthData1.getPressure();
            maxHrv = Math.max(maxHrv, hrv);
            minHrv = Math.min(minHrv, hrv);
            maxPressure = Math.max(maxPressure, pressure);
            minPressure = Math.min(minPressure, pressure);
        }
        Collections.sort(healthDataList, new BloodMaxComparator());
        HealthData maxBlood = healthDataList.get(healthDataList.size() - 1);
        maxBloodHigh = maxBlood.getHighBloodPressure();
        maxBloodLow = maxBlood.getLowBloodPressure();
        Collections.sort(healthDataList, new BloodMinComparator());
        HealthData maxMin = healthDataList.get(0);
        int avgHigh = maxMin.getHighBloodPressure();
        int avgLow = maxMin.getLowBloodPressure();
        tvBloodMaxValue.setText(String.valueOf(maxBloodHigh) + "/" + String.valueOf(maxBloodLow) + " mmHg");
        tvBloodAvgValue.setText(String.valueOf(avgHigh) + "/" + String.valueOf(avgLow) + " mmHg");
    }

    private void showHealthData(int position) {
        HealthData healthData = todayHealths.get(position);
        HealthData lastHealthData = position < todayHealths.size() - 1 ? todayHealths.get(position + 1) : todayHealths.get(position);
        int hrv = healthData.getHrv();
        int pressure = healthData.getPressure();
        int highPressure = healthData.getHighBloodPressure();
        int lowPressure = healthData.getLowBloodPressure();
        int avblock = healthData.getBloodValue();
        tvBloodData.setText(String.valueOf(highPressure) + "/" + String.valueOf(lowPressure) + "\nmmHg");
        tvHrvValue.setText(String.valueOf(hrv));
        tvStressValue.setText(String.valueOf(pressure));
        setHrvLevel(hrv, lastHealthData);
        setPressureLevel(pressure, lastHealthData);
        setBlockLevel(avblock, lastHealthData);
    }

    private void clearData() {
        tvBloodData.setText("--" + "/" + "--" + "\n"+getResources().getString(R.string.bp_day_level));
        tvBloodMaxValue.setText("--" + "/" + "--" + " "+getResources().getString(R.string.bp_day_level));
        tvBloodAvgValue.setText("--" + "/" + "--" + " "+getResources().getString(R.string.bp_day_level));
        tvHrvValue.setText("--");
        tvAvblockValue.setText("--");
        tvStressValue.setText("--");
        tvStressAdvice.setText("");
        tvHrvAdvice.setText("");
        setDrawableRight(0, tvHrvValue);
        setDrawableRight(0, tvAvblockValue);
        setDrawableRight(0, tvStressValue);
        RadioGroupStress.clearCheck();
        ivHrvLevel0.setVisibility(View.INVISIBLE);
        ivHrvLevel1.setVisibility(View.INVISIBLE);
        ivHrvLevel2.setVisibility(View.INVISIBLE);
        ivHrvLevel3.setVisibility(View.INVISIBLE);
    }

    private void setBlockLevel(int avblock, HealthData healthData) {
        tvAvblockValue.setText(blockLevels[avblock]);
        ivAvBlock.setImageLevel(avblock);
        if (healthData != null) {
            int lastValue = healthData.getBloodValue();
            setDrawableRight(avblock < lastValue ? R.drawable.data_down : R.drawable.data_up, tvAvblockValue);
        }
    }

    private void setHrvLevel(int hrv, HealthData lastHealthData) {
        int hrvLevel = HealthDataUtils.getHrvLevel(hrv);
        String levelHrv = "--";
        int color = Color.WHITE;
        levelHrv = hrvContent[hrvLevel];
        color = hrvColor[hrvLevel];
        String adviceHRV = hrvAdvice[hrvLevel];
        tvHrvAdvice.setText(adviceHRV);
        ivHrvLevel0.setVisibility(hrvLevel == 3 ? View.VISIBLE : View.INVISIBLE);
        ivHrvLevel1.setVisibility(hrvLevel == 2 ? View.VISIBLE : View.INVISIBLE);
        ivHrvLevel2.setVisibility(hrvLevel == 1 ? View.VISIBLE : View.INVISIBLE);
        ivHrvLevel3.setVisibility(hrvLevel == 0 ? View.VISIBLE : View.INVISIBLE);
        String hrvValue = String.valueOf(hrv == 0 ? "--" : hrv + " " + levelHrv);
        if (lastHealthData != null) {
            int lastValue = lastHealthData.getHrv();
            Log.i(TAG, "setHrvLevel: " + lastHealthData.getTime() + " " + lastHealthData.getHrv());
            setDrawableRight(hrv < lastValue ? R.drawable.data_down : R.drawable.data_up, tvHrvValue);
        }
        tvHrvValue.setText(hrvValue);
        tvHrvValue.setTextColor(color);
    }


    private void setDrawableRight(int id, TextView textView) {
        if (id == 0) {
            textView.setCompoundDrawables(null, null, null, null);
            return;
        }
        Drawable drawable = getResources().getDrawable(id);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    private void setPressureLevel(int pressure, HealthData healthData) {
        String levelHrv = "--";
        int color = Color.WHITE;
        int id = R.id.RadioButton_stress1;
        int stressLevel = HealthDataUtils.getStressLevel(pressure);
        String stressAdviceString = stressAdvice[stressLevel];
        levelHrv = stressContent[stressLevel];
        color = hrvColor[stressLevel];
        id = getResources().getIdentifier("RadioButton_stress" + (stressLevel + 1),
                "id", getActivity().getPackageName());
        if (healthData != null) {
            int lastValue = healthData.getPressure();
            setDrawableRight(pressure < lastValue ? R.drawable.data_down : R.drawable.data_up, tvStressValue);
        }
        RadioGroupStress.check(id);
        tvStressAdvice.setText(stressAdviceString);
        String stressValue = String.valueOf(pressure == 0 ? "--" : pressure + " " + levelHrv);
        tvStressValue.setText(stressValue);
        tvStressValue.setTextColor(color);
    }

    private void initHeartView(String queryDate) {
        List<HeartData> heartDataList = HeartDataDaoManager.queryDayData(queryDate.substring(2));
        int maxDayHr = 0;
        int minDayHr = 300;
        int totalDayHr = 0;
        int count = 0;

        int offsetCount = 5;
        int firstCount = -1;
        // HashMap<Integer, Integer> offsetMap = new HashMap<>();
        for (int i = 0; i < heartDataList.size(); i++) {
            HeartData heartData = heartDataList.get(i);
            // String date = heartData.getTime();
            //    int startCount = DateUtil.getMIndex(date, queryDate.split(" ")[0] + " 00:00:00", 1);
            int heartRate = heartData.getHeart();
            // if (startCount >= 1440) break;
            if (heartRate != 0) {
//                if (firstCount != -1) {
//                    offsetCount = startCount - firstCount;
//                    Integer offset = offsetMap.get(offsetCount);
//                    if (offset == null) {
//                        offsetMap.put(offsetCount, 1);
//                    } else {
//                        offset++;
//                        offsetMap.put(offsetCount, offset);
//                    }
//                }
//                firstCount = startCount;
                count++;
                totalDayHr += heartRate;
                maxDayHr = Math.max(maxDayHr, heartRate);
                minDayHr = Math.min(minDayHr, heartRate);
            }

        }
//        int maxOffset = 0;
//        int drawOffset = 5;
//        for (Map.Entry<Integer, Integer> entry : offsetMap.entrySet()) {
//            if (entry.getValue() > maxOffset) {
//                maxOffset = entry.getValue();
//                drawOffset = entry.getKey();
//            }
//        }
//        if (drawOffset < 5) drawOffset = 5;
//        if (drawOffset > 60) drawOffset = 60;
        int[] heartArray = new int[1440];//24*60分钟
        for (int i = 0; i < heartDataList.size(); i++) {
            HeartData heartData = heartDataList.get(i);
            String date = heartData.getTime();
            int startCount = DateUtil.getMIndex(date, queryDate.split(" ")[0] + " 00:00:00", 1);
            int heartRate = heartData.getHeart();
            if (startCount >= 1440) break;
            if (heartRate != 0) {
                heartArray[startCount] = heartRate;
            }
        }

        float top = 0;
        if (maxDayHr % 10 == 0) {
            top = maxDayHr;
        } else {
            top = (((int) maxDayHr / 10) + 1) * 10;
        }

        ChartDataUtil.initDataChartView(LineChartViewHealthHeart, -0.5f, top + 10, heartArray.length, 30);
        LineChartViewHealthHeart.setLineChartData(ChartDataUtil.getHistoryHeartDayChart(getActivity(), heartArray, mFace, 0, top));
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_date, R.id.bt_ecg, R.id.iv_hrv_data, R.id.iv_stress_data, R.id.tv_health_blood, R.id.tv_health_heart, R.id.tv_health_hr, R.id.tv_blood_pressure, R.id.tv_hrv, R.id.tv_body_pressure, R.id.tv_AV_BLOCK})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_ecg:
                startTipsActivity(HeartTipsActivity.TipsType_ECG);
                break;
            case R.id.ll_date:
                if (CalendarLayout.isExpand()) {
                    CalendarLayout.shrink();
                    ivDate.setRotation(0);
                } else {
                    ivDate.setRotation(180);
                    ScrollView.fling(0);
                    ScrollView.smoothScrollTo(0, 0);
                    CalendarLayout.expand();
                }
                break;
            case R.id.tv_health_hr:
                startTipsActivity(HeartTipsActivity.TipsType_Heart);
                break;
            case R.id.tv_blood_pressure:
                startTipsActivity(HeartTipsActivity.TipsType_BLOOD);
                break;
            case R.id.tv_hrv:
                startTipsActivity(HeartTipsActivity.TipsType_HRV);
                break;
            case R.id.tv_body_pressure:
                startTipsActivity(HeartTipsActivity.TipsType_Stress);
                break;
            case R.id.tv_AV_BLOCK:
                break;
            case R.id.tv_health_heart:
                startActivity(HeartHistoryActivity.class, HealthHistoryActivity.KEY_DATA_DATE, findDate.substring(2));
                break;
            case R.id.tv_health_blood:
                startHealthActivity(HealthHistoryActivity.DATA_BLOOD);
                break;
            case R.id.iv_hrv_data:
                startHealthActivity(HealthHistoryActivity.DATA_HRV);
                break;
            case R.id.iv_stress_data:
                startHealthActivity(HealthHistoryActivity.DATA_PRESSURE);
                break;
        }
    }

    private void startHealthActivity(int type) {
        Intent intent = new Intent(getActivity(), HealthHistoryActivity.class);
        intent.putExtra(HistoryActivity.KEY_DATA_TYPE, type);
        intent.putExtra(HealthHistoryActivity.KEY_DATA_DATE, findDate.substring(2));
        startActivity(intent);
    }

    private void startTipsActivity(int type) {
        Intent intent = new Intent(getActivity(), HeartTipsActivity.class);
        intent.putExtra(HeartTipsActivity.TipsType, type);
        startActivity(intent);
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
